﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SMBCFamilyAssessment.Models;
using System.Data;
using System.Diagnostics;
using System.Data.Entity;
using Itenso.TimePeriod;
using System.Data.Entity.Core.Objects;
using System.Collections;
namespace SMBCFamilyAssessment.Logic
{


    public class AssessmentBLcs
    {
        public ChartData GetData(DateTime assessmentDate, int personId)
        {

            GetAssessmentData(3);
            ChartData cData = new ChartData();

            // Debug.Write("********GetData Called for the assessment date " + assessmentDate1 + Environment.NewLine);

            Dictionary<int, string> QuestionsDictionary = new Dictionary<int, string>();
            Dictionary<int, int> ScoresDictionary = new Dictionary<int, int>();


            using (SMBCFamilyAssessment.Models.AssessmentContext _db = new SMBCFamilyAssessment.Models.AssessmentContext())
            {



                var query = from p in _db.Persons
                            join s in _db.AssessmentSessions
                            on p.PersonId equals s.PersonId
                            join q in _db.Questions
                            on s.QuestionId equals q.QuestionId
                            join a in _db.Assessments
                            on s.AssessmentSessionDate equals a.AssessmentDate
                            where p.PersonId.Equals(personId) && a.PersonId.Equals(s.PersonId) && a.AssessmentDate.Equals(assessmentDate)


                            select new
                            {
                                p.PersonId,
                                p.LastName,
                                p.DOB,
                                s.AssessmentSessionDate,
                                s.QuestionId,
                                q.QuestionDescription,
                                s.Score,
                                a.AssessmentDate
                            };





                foreach (var s in query)
                {

                    QuestionsDictionary.Add(s.QuestionId, s.QuestionDescription);

                    ScoresDictionary.Add(s.QuestionId, s.Score);

                    Debug.Write(s.PersonId + " " + s.LastName + " " + s.DOB + "   " + s.AssessmentSessionDate + "   " + s.QuestionId + "    "
                        + "    " + s.QuestionDescription + "    " + s.Score + Environment.NewLine);



                }


                foreach (var item in QuestionsDictionary)
                {

                    Debug.Write("  Question ID  " + item.Key + "  QuestionDescription  " + item.Value + Environment.NewLine);


                }


                foreach (var item in ScoresDictionary)
                {

                    Debug.Write("  Question ID  " + item.Key + "  Score  " + item.Value + Environment.NewLine);

                }

                cData.QuestionsDictionary = QuestionsDictionary;
                cData.ScoresDictionary = ScoresDictionary;



                return cData;




            }


        }
        
        public static bool InsertAssessment(Dictionary<int,SMBCFamilyAssessment.Models.AssessmentSession> sessions)
        {
             using (SMBCFamilyAssessment.Models.AssessmentContext _db = new SMBCFamilyAssessment.Models.AssessmentContext())
                {
                  try
                   {
              
                    SMBCFamilyAssessment.Models.Assessment assessmentobj = new SMBCFamilyAssessment.Models.Assessment();


                    //review and pass additional assessment related parameters from view state
                    assessmentobj.AssessmentDate = DateTime.Now.Date.Date;
                    assessmentobj.AssessmentTypeId = 1;
                    assessmentobj.DateCreated = DateTime.Now.Date.Date;
                    assessmentobj.PersonId = 1;
                    _db.Assessments.Add(assessmentobj);

                    foreach (var item in sessions)
                    {


                        _db.AssessmentSessions.Add(item.Value);
                        Debug.Write("***********Results**************" + Environment.NewLine);
                        Debug.Write(" Person ID:" + item.Value.PersonId + Environment.NewLine);
                        Debug.Write(" Assessment Session Date: " + item.Value.AssessmentSessionDate + Environment.NewLine);
                        Debug.Write(" Assessment Type ID " + item.Value.AssessmentTypeId + Environment.NewLine);
                        Debug.Write(" Q1D " + item.Value.QuestionId + Environment.NewLine);
                        Debug.Write(" Score " + item.Value.Score + Environment.NewLine);

                        Debug.Write("***********Results**************" + Environment.NewLine);



                    }
                    _db.SaveChanges();
                }
                
          
                catch(Exception e)
                {

                    return false;

                }
               return false;
            }
        }






        public List<DateTime> getMaxDates(int personId, int assessmentTypeId)
        {
            using (SMBCFamilyAssessment.Models.AssessmentContext _db = new SMBCFamilyAssessment.Models.AssessmentContext())
            {


                List<DateTime> maxDateList = new List<DateTime>();
                IQueryable<Assessment> MaxDates = _db.Assessments
                    .OrderByDescending(a => a.AssessmentDate)
                    .Where(a => a.PersonId == personId && a.AssessmentTypeId == assessmentTypeId).Take(2);


                foreach (var item in MaxDates)
                {

                    maxDateList.Add(item.AssessmentDate);
                    Debug.Write("date is " + item.AssessmentDate + Environment.NewLine);

                }


                return maxDateList;



            }
        }
        public static List<Family> GetPropertyAddressData(string s)
        {

            if (!string.IsNullOrEmpty(s))
            {
                string fs = s.Replace("%", "_");

                using (AssessmentContext _db = new SMBCFamilyAssessment.Models.AssessmentContext())
                {

                    var q = from p in _db.Families

                            where p.PostCode.StartsWith(fs)
                            select p;




                    foreach (Family pa in q)
                    {




                    }


                    return q.ToList<Family>();

                }

            }

            return null;

        }
        public AssessmentData GetAssessmentData(int AssessmentTypeId)
        {

        Dictionary<int, string> QuestionsetDictionary = new Dictionary<int, string>();
        Dictionary<int, string> QuestionDictionary = new Dictionary<int, string>();
        Dictionary<int, int> QuestionAndQuestionSetDictionary = new Dictionary<int, int>();
            //replace assessment id & person Id with session variables

        AssessmentData assessmentData = new AssessmentData(AssessmentTypeId,1);
        
            using (AssessmentContext _db = new SMBCFamilyAssessment.Models.AssessmentContext())
            {

                var query = from t in _db.AssessmentTypes
                            join s in _db.AssessmentSpecifications
                            on t.AssessmentTypeId equals s.AssessmentTypeId
                            join qs in _db.QuestionSets
                            on s.QuestionSetId equals qs.QuestionSetId
                            where t.AssessmentTypeId.Equals(AssessmentTypeId) 


                            select new
                            {
                              qs.QuestionSetId,qs.QuestionSetDescription
                             
                           };

                var query2 = from t in _db.AssessmentTypes
                            join s in _db.AssessmentSpecifications
                            on t.AssessmentTypeId equals s.AssessmentTypeId
                            join q in _db.Questions
                            on s.QuestionSetId equals q.QuestionSetId
                            where t.AssessmentTypeId.Equals(AssessmentTypeId)


                            select new
                            {
                                q.QuestionId,
                                q.QuestionDescription,
                                 //may need a review
                                q.QuestionSetId
                                 //may need a review

                            };


                foreach (var s in query)
                {

                    QuestionsetDictionary.Add(s.QuestionSetId, s.QuestionSetDescription);
                  
                    Debug.Write(s.QuestionSetId + "*** " + s.QuestionSetDescription  +Environment.NewLine);

                 }


                foreach (var s in query2)
                {

                    QuestionDictionary.Add(s.QuestionId, s.QuestionDescription);
                      
                    //may need a review
                    QuestionAndQuestionSetDictionary.Add(s.QuestionId, s.QuestionSetId);
                    //may need a review


                    
                    Debug.Write(s.QuestionId+ "*** " + s.QuestionDescription + Environment.NewLine);



                }
                foreach (var item in QuestionsetDictionary)
                {

                    Debug.Write("  Question Set ID  " + item.Key + "  *QuestionSetDescription*  " + item.Value + Environment.NewLine);


                }


                foreach (var item in QuestionDictionary)
                {

                    Debug.Write("  Question ID  " + item.Key + "  *Question Desc*  " + item.Value + Environment.NewLine);

                }

                assessmentData.QuestionDictionary = QuestionDictionary;
                assessmentData.QuestionsetDictionary = QuestionsetDictionary;
                //may need a review
                assessmentData.QuestionAndQuestionSetDictionary = QuestionAndQuestionSetDictionary;

                //may need a review
                
           }


            return assessmentData;

        }

    

    }
}